package com.cet.qz.unit01.chapter1_3;

/**
 * @program: algorithm
 * @description: 打印出队列中倒数第k个字符串
 * @author: 陈恩涛
 * @create: 2021-04-09 21:56
 **/
public class Qz1_3_15 {

    public static void printLastKElementInQueue(Queue<String> queue, int num) {
        if (queue.size() < num) {
            throw new RuntimeException("index out bound of queue!");
        }
        num = queue.size() - num;
        for (String s : queue) {
            if (num == 0) {
                System.out.println(s);
            }
            num--;
        }
    }

    public static void main(String[] args) {
        Queue<String> queue = new Queue<>();
        queue.enqueue("a");
        queue.enqueue("b");
        queue.enqueue("c");
        queue.enqueue("d");
        queue.enqueue("e");
        queue.enqueue("f");
        printLastKElementInQueue(queue,2);
    }
}
